package
{
	import code.app.AppData;
	import code.app.AppDmList;
	import code.common.assets.AssetsClassName;
	import code.common.assets.AssetsKey;
	import code.main.modle.config.ConfigData;
	import code.module.loading.LoadingCtrl;
	import code.phpnet.GlobalConfig;
	
	import com.yy.base.App;
	import com.yy.manager.ResourceManager;
	import com.yy.tools.load.LoadSwfParamInfo;
	import com.yy.tools.load.LoadSwfToLoacal;
	import com.yy.tools.load.LoaderXML;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.system.Security;
	
	/**
	 *加载类 
	 * @author YY
	 * 
	 */
	[SWF(width="760", height="680", backgroundColor="0xffffff", frameRate="24")]
	public class Loading extends Sprite
	{
		private var _loadCtrl:LoadingCtrl;//加载动画控制器
		private var _game:*;//主程序
		public function Loading()
		{
			super();
			Security.allowDomain("*");
			if(this.stage) {
				init();
			}else {
				this.addEventListener(Event.ADDED_TO_STAGE, addedToStageFunc);
			}
		}

		/**
		 *初始化舞台 
		 * @param e
		 * 
		 */
		private function addedToStageFunc(e:Event):void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageFunc);
			init();
		}
		
		/**
		 *初始化程序开始 
		 * 
		 */
		private function init():void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			//初始化app
			App.appStage = this.stage;
			App.appDomain = ApplicationDomain.currentDomain;
			AppData.getInstance().swfVar = this.stage.loaderInfo.parameters;
			var pr:Object = AppData.getInstance().swfVar;
			ResourceManager.currentDomain = ApplicationDomain.currentDomain;
			_loadCtrl = new LoadingCtrl(this);
			var globalParam:GlobalConfig = GlobalConfig.instance;
			function checkParams(str:String):Boolean {
				return typeof(stage.loaderInfo.parameters[str]) != 'undefined';
			}
			if(!GlobalConfig.IS_DEBUG) {
				globalParam.mid = checkParams('uid') ? pr.uid : 0;
				globalParam.sid=checkParams('pfid') ? pr.pfid : 1;
				globalParam.mtkey = checkParams('olkey') ? pr.olkey : "";//key
				globalParam.unid = checkParams('usid') ? pr.usid : '';//子站点id
				globalParam.langtype = checkParams('lang') ? pr.lang : '';//语言类型			
				globalParam.gateway = checkParams('gwurl') ? pr.gwurl : '';
				globalParam.tid = checkParams('tid') ? pr.tid : 0;//桌子id
				globalParam.pic = checkParams('uface') ? pr.uface : '';//头像地址
				globalParam.basUrl = checkParams('baseurl') ? pr.baseurl : '';//首地址
				globalParam.flashurl= checkParams('swfurl') ? pr.swfurl : '';//加载swf地址
				globalParam.xmlurl= checkParams('xmlurl') ? pr.xmlurl : '';//加载swf地址
				globalParam.imgurl= checkParams('imgurl') ? pr.imgurl : '';//加载图片地址
				globalParam.xver= checkParams('xver') ? pr.xver : '';//加载图片地址
				globalParam.swfver= checkParams('fver') ? pr.fver : '';//加载图片地址
			}else {
				globalParam.mid = 1;
				globalParam.sid=1;
				globalParam.mtkey = "123456";
				globalParam.unid = "1";
				globalParam.langtype = "1";//语言类型			
				globalParam.gateway ="http://127.0.0.1:89/trunk/gw.php";
				globalParam.tid = 0;//桌子id
				globalParam.pic = "";//头像地址
				globalParam.basUrl = "";//首地址
				globalParam.flashurl= "";//加载swf地址
				globalParam.xmlurl= "";//加载swf地址
				globalParam.imgurl= "";//加载图片地址
				globalParam.xver= "";//加载图片地址
				globalParam.swfver= "";//加载图片地址
			}
			loadConfig();
		}
		
		/**
		 *加载配置文件 
		 * 
		 */
		private function loadConfig():void {
			var lxml:LoaderXML = new LoaderXML();
			if(GlobalConfig.IS_DEBUG) {
				lxml.loadXml("ini.xml", loadConfiCmp);
			}else {
				trace("lxml：" + GlobalConfig.instance.xmlurl);
				lxml.loadXml(GlobalConfig.instance.xmlurl + "ini.xml", loadConfiCmp);
			}
			
		}
		/**
		 *配置文件加载成功 
		 * @param xml
		 * 
		 */
		private function loadConfiCmp(xml:XML):void {
			AppData.getInstance().initConfi = xml;
			AppDmList.getInstance().configDm.parse(xml);
			preLoadSwf();
		}
		
		/**
		 *预加载swf到本地 
		 * 
		 */
		private function preLoadSwf():void {
			var dm:ConfigData = AppDmList.getInstance().configDm; 
			new LoadSwfToLoacal().loadSwf(dm.getSwfInofs(dm.preLoadKeys), preLoadCmp, preLoadProgress);
		}
		
		/**
		 *预加载资源完成  加载组程序壳
		 * 
		 */
		private function preLoadCmp():void {
			trace("预加载资源完成");
			var initMain:Object = ResourceManager.getObjectByName(AssetsClassName.INIT_MAIN);
			initMain.callback = loadMainCmp;
			initMain.start();
		}
		
		/**
		 *初始化主程序
		 * @param main
		 * 
		 */
		private function loadMainCmp(main:Class):void {
			_game = new main();
			_game.init(gameInitCmp);
		}
		
		/**
		 *主程序初始化完成 
		 * 
		 */
		private function gameInitCmp():void {
			if(AppData.getInstance().isFirstLogin) {
				new LoadSwfToLoacal().loadSwf([AppDmList.getInstance().configDm.getSwfInofs([AssetsKey.KEY_GUID])], loadGuidCmp, preLoadProgress);
			}else {
				gameRun();
			}
		}
		
		private function loadGuidCmp():void {
			trace("新手向导加载完成");
			gameRun();
		}
		
		/**
		 *一切初始化工作完成 开始游戏 
		 * 
		 */
		public function gameRun():void {
			App.appStage.addChild(DisplayObject(this._game));
			this._game.run();
			dispose();
			trace("开始游戏");
		}
		
		/**
		 *释放加载器 
		 * 
		 */
		public function dispose():void{
			_loadCtrl.dispose();
			_game = null;
			App.appStage.removeChild(this);
			trace("加载器释放完毕");
		}
		
		
		/**
		 *加载ui资源 时对进度条的控制 
		 * @param data
		 * 
		 */
		private function preLoadProgress(data:Object):void
		{
			var totalBytes:int = data.totalBytes;
			var bytesLoaded:int = data.bytesLoaded;
			_loadCtrl.setLoadData(bytesLoaded, totalBytes);
		}
		
		
	}
}